#
# @lc app=leetcode.cn id=111 lang=python3
#
# [111] 二叉树的最小深度
#
import collections


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
# @lc code=start
# Definition for a binary tree node.

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if root is None:
            return 0
        deque = collections.deque()
        deque.append(root)
        depth = 0
        while deque:
            size = len(deque)
            depth += 1
            while size > 0:
                size -= 1
                node = deque.popleft()
                if node.left is None and node.right is None:
                    return depth
                if node.left:
                    deque.append(node.left)
                if node.right:
                    deque.append(node.right)
        
            
# @lc code=end